/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GroupUpdatePanel.java
 *
 * Created on Oct 22, 2014, 4:56:41 PM
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.AksesMatrixDAO;
import com.ivanbiz.dao.GroupsDAO;
import com.ivanbiz.dao.impl.AksesMatrixDAOImpl;
import com.ivanbiz.dao.impl.GroupsDAOImpl;
import com.ivanbiz.model.AksesMatrix;
import com.ivanbiz.model.Groups;
import com.ivanbiz.service.JTextFieldLimit;
import com.ivanbiz.service.MenuAksesConstant;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author IW20149X
 */
public class GroupUpdatePanel extends javax.swing.JPanel {

    List<AksesMatrix> listAksesMatrix;
    List<AksesMatrix> listAksesMatrixView;
    Groups groups;
    JDialog jDialog;

    public GroupUpdatePanel(JDialog jDialog) {
        initComponents();
        listAksesMatrix = new ArrayList<AksesMatrix>();
        listAksesMatrixView = new ArrayList<AksesMatrix>();
        renderTableAkses();
        this.jDialog = jDialog;
    }

    public GroupUpdatePanel(JDialog jDialog, Groups groups) {
        try {
            initComponents();
            this.groups = groups;
            labelGroup.setText("Ubah Group");
            textNama.setText(groups.getNama());
            this.jDialog = jDialog;
            AksesMatrixDAO aksesMatrixDAO = new AksesMatrixDAOImpl();
            listAksesMatrix = aksesMatrixDAO.getDataByEquals(AksesMatrix.class, "groups.id", groups.getId());
            listAksesMatrixView = new ArrayList<AksesMatrix>();
            renderTableAkses();
        } catch (Exception ex) {
            Logger.getLogger(GroupUpdatePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelGroup = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textNama = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableHakAkses = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        labelGroup.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelGroup.setText("Tambah Group Baru");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Nama :");

        textNama.setDocument(new JTextFieldLimit(50));

        jLabel3.setText("Menu Akses ");

        tableHakAkses.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableHakAkses);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(textNama, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelGroup, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (groups == null) {
            groups = new Groups();
        }
        groups.setNama(textNama.getText());
        validate(groups);
}//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        this.jDialog.dispose();
}//GEN-LAST:event_buttonBatalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelGroup;
    private javax.swing.JTable tableHakAkses;
    private javax.swing.JTextField textNama;
    // End of variables declaration//GEN-END:variables

    private void renderTableAkses() {
        for (MenuAksesConstant menuAksesConstant : MenuAksesConstant.values()) {
            AksesMatrix aksesMatrix = new AksesMatrix();
            aksesMatrix.setNama(menuAksesConstant.toString());
            aksesMatrix.setMenuChecked(MenuAksesConstant.validate(menuAksesConstant, listAksesMatrix));
            listAksesMatrixView.add(aksesMatrix);
        }
        updateTableAkses();
    }

    private void updateTableAkses() {
        String[] judul = {"Choose", "Nama"};
        Object[][] isi = new Object[listAksesMatrixView.size()][2];
        int x = 0;
        for (AksesMatrix aksesMatrix : listAksesMatrixView) {
            isi[x][0] = aksesMatrix.isMenuChecked();
            isi[x][1] = aksesMatrix.getNama();
            x++;
        }

        tableHakAkses.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return columnIdentifiers.get(columnIndex).equals("Choose");
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIdentifiers.get(columnIndex).equals("Choose")) {
                    return Boolean.class;
                } else {
                    return columnIdentifiers.get(columnIndex).getClass();
                }
            }
        });

        TableColumn column0 = tableHakAkses.getColumnModel().getColumn(0);
        column0.setMaxWidth(50);
    }

    private void validate(Groups groups) {
        if (groups == null) {
            JOptionPane.showMessageDialog(this, "Group tidak boleh null");
        } else if (groups.getNama() == null) {
            JOptionPane.showMessageDialog(this, "Nama tidak boleh null");
        } else if (groups.getNama().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Nama tidak boleh kosong");
        } else {
            try {
                AksesMatrixDAO aksesMatrixDAO = new AksesMatrixDAOImpl();
                if (labelGroup.getText().equals("Ubah Group")) {
                    List<AksesMatrix> list = aksesMatrixDAO.getDataByEquals(AksesMatrix.class, "groups.id", groups.getId());
                    for (AksesMatrix aksesMatrix : list) {
                        aksesMatrixDAO.delete(aksesMatrix);
                    }
                }
                List<AksesMatrix> listAksesMatrixs = new ArrayList<AksesMatrix>();
                for (int x = 0; x < listAksesMatrixView.size(); x++) {
                    AksesMatrix aksesMatrixs = listAksesMatrixView.get(x);
                    if ((Boolean) tableHakAkses.getValueAt(x, 0)) {
                        aksesMatrixs.setGroups(groups);
                        listAksesMatrixs.add(aksesMatrixs);
                    }
                }
                GroupsDAO groupsDAO = new GroupsDAOImpl();
                groupsDAO.saveOrUpdate(groups, listAksesMatrixs);
                this.jDialog.dispose();
            } catch (Exception ex) {
                Logger.getLogger(GroupUpdatePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
